package protocol

import (
	"golang.org/x/net/websocket"
	"sync"
)

type WebsocketConnection struct {
	ConnId string
	Conn   *websocket.Conn
}

var ClientConnList = newWebsocketConnectionMapList()

func newWebsocketConnectionMapList() *websocketConnectionMapList {
	return &websocketConnectionMapList{}
}

type websocketConnectionMapList struct {
	connList sync.Map //clients map[string]*WebsocketConnection
}

func (c *websocketConnectionMapList) Store(conn *WebsocketConnection) {
	c.connList.Store(conn.ConnId, conn)
}

func (c *websocketConnectionMapList) Delete(connId string) {
	c.connList.Delete(connId)
}

func (c *websocketConnectionMapList) Range(f func(conn *WebsocketConnection) bool) {
	c.connList.Range(func(key, value interface{}) bool {
		return f(value.(*WebsocketConnection))
	})
}
